<?php

namespace backend\module\shop\models;

use Yii;
use backend\models\Node;
/**
 * This is the model class for table "{{%goods_category}}".
 *
 * @property int $id
 * @property int $parent
 * @property string $name
 * @property string $icon
 * @property int $sort
 * @property int $userid
 * @property string $keywords
 * @property string $description
 */
class GoodsCategory extends Node
{
    static protected  $_data;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods_category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parent', 'sort', 'userid'], 'integer'],
            [['description'], 'string'],
            [['name'], 'string', 'max' => 100],
            [['icon', 'keywords'], 'string', 'max' => 255],
        ];
    }
    /**
     * 返回所有分类数据
     * @param int $userid  分类所属的用户ID
     * @return array 
     */
    static public function getAllData($userid){
        $data = self::$_data[$userid];
        if(empty($data)){
           //$data = self::find(['userid'=>$userid])->asArray()->all();
           $data = self::findAll(['userid'=>$userid]);
           self::$_data[$userid] = $data;
        }
        return $data;
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'parent' => Yii::t('common', 'Parent'),
            'name' => Yii::t('common', 'Category Name'),
            'icon' => Yii::t('common', 'Category Icon'),
            'sort' => Yii::t('common', 'Sort'),
            'userid' => Yii::t('common', 'Userid'),
            'keywords' => Yii::t('common', 'Keywords'),
            'description' => Yii::t('common', 'Description'),
        ];
    }
}
